<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\LogisticsTemplate;
use App\Api\Helpers\ApiResponse;
use App\Models\Logistics;
use App\Models\LogisticsTemplate as LogisticsTemplateModel;
use Cblink\Region\Region;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;

class LogisticsTemplateController extends AdminController
{

    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description()['create'] ?? trans('admin.create'))
            ->body($this->form(request()->input('logistics_id')));
    }

    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->form(request()->input('logistics_id'))->edit($id));
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($logistics_id = 0)
    {
        return Form::make(
            new LogisticsTemplate(),
            function (Form $form) use ($logistics_id) {
                $form->text('name');
                $form->hidden('logistics_id')->default($logistics_id);
                $form->radio('type')->options(LogisticsTemplateModel::$typeMap)
                    ->default(LogisticsTemplateModel::TYPE_DEFAULT_ADDRESS);
//                $form->tree('address_ids')
//                    ->nodes((new Region())->nest())
//                    ->customFormat(
//                        function ($v) { // 格式化外部注入的值
//                            if (!$v) {
//                                return [];
//                            }
//
//                            return array_column($v, 'id');
//                        }
//                    );
                $form->divider();
                $form->radio('open_piece')->options([0 => '否', 1 => '是'])->default(1);
                $form->number('first_piece')->default(0);
                $form->decimal('first_piece_price')->default(0);
                $form->number('other_piece')->default(0);
                $form->decimal('other_piece_price')->default(0);

                $form->divider();
                $form->radio('open_weight')->options([0 => '否', 1 => '是'])->default(1);
                $form->number('first_weight')->default(0);
                $form->decimal('first_weight_price')->default(0);
                $form->number('other_weight')->default(0);
                $form->decimal('other_weight_price')->default(0);

                $form->divider();
                $form->radio('open_volume')->options([0 => '否', 1 => '是'])->default(1);
                $form->number('first_volume')->default(0);
                $form->decimal('first_volume_price')->default(0);
                $form->number('other_volume')->default(0);
                $form->decimal('other_volume_price')->default(0);


                $form->saving(
                    function (Form $form) {
                        if (Logistics::query()->where('id', $form->logistics_id)->doesntExist()) {
                            return $form->error('参数异常');
                        }
                    }
                );

                // 跳转并提示成功信息
                $form->saved(
                    function (Form $form) {
                        return $form->redirect(
                            route('admin.logistics.index'),
                            [
                                'message' => '添加物流模板成功',
                            ]
                        );
                    }
                );

                $form->disableListButton();
                $form->disableViewButton();
            }
        );
    }

    // 修改[open_piece, open_weight, open_volume]状态
    public function status(LogisticsTemplateModel $logisticsTemplate, Request $request)
    {
        $types = ['piece', 'weight', 'volume'];
        if (!in_array($type = $request->input('type'), $types)) {
            return admin_error('类型不存在');
        }

        $attribute = "open_{$type}";
        $logisticsTemplate->update(
            [
                $attribute => !$logisticsTemplate->{$attribute},
            ]
        );
    }
}
